

const express = require('express')
const app = express()
var cookieParser = require('cookie-parser');  
app.use(cookieParser());  
const port = 3000
app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
     res.header("Access-Control-Allow-Credentials", true);
    res.header("Access-Control-Allow-Headers", "X-Requested-With,content-type,x-access-token,Authorization,Accept,withCredentials,platform");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    if (req.method == 'OPTIONS') {
        res.sendStatus(204);
    } else {
        next();
    }
    
})
app.get('/jsonp', (req, res) => {
    //sql mongodb
    let arr = [1, 2, 3, 4, 5];
    //获取客户端传递的回调函数名称
    let callback = req.query.jsonpCallback;
    //let callback = function (data) { console.log(data); }
    //执行callbakc，并且进行传递参数
    res.send(callback + '(' + JSON.stringify(arr) + ')');

});
app.get('/home', (req, res) => {
    try{
        console.log(req.cookies.username)

        res.send('home');
    }
    catch(e){
        console.log(e.message);
        res.send('e.message');
    }
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`))